
import sys
import os.path 

cur_path = os.path.dirname(__file__)
parent_path = os.path.abspath(os.path.join(cur_path, '..'))
sys.path.append(parent_path)

import redis
import pymongo

import time
import re
import urllib

findloginfo = re.compile('^([\d.]*) say\.23201\.com \[([^ ]* [\d+]*)\] "GET ([^\b\"]*) HTTP/1.\d" (\d*) ')

def parse_line(log_line):
    try:
        loginfo = findloginfo.findall(log_line)
        host, timestamp, req, httpcode = loginfo[0]
        req = req.lstrip('/url?url=')
        for i in req.split('?')[1].split('&'):
            if 'seller_nick' in i:
                seller_nick = i
            if 'id' in i:
                numiid = i
        seller_nick = urllib.unquote(seller_nick.split('=')[1])
        numiid = numiid.split('=')[1]
    except:
        return None
    return (seller_nick.strip(), numiid)


def click_stat_internal(fn, rdb):
    for line in fn:
        r = parse_line(line)
        if not r:
            continue
        seller_nick, numiid = r
        rdb.hincrby(seller_nick, numiid, 1)


def click_stat(fn, rdb, mdb):
    click_stat_internal(fn, rdb)
    daystr = time.strftime('%Y%m%d', time.localtime((time.time()-86400)))
    for i in rdb.keys():
        stres = rdb.hvals(i)
        keystr = ':'.join([i, daystr])
        clicks = sum([ int(j) for j in stres])
        mdb.dailyclick.save({'_id':keystr, 'click':clicks})
        ts = mdb.totalstat.find_one({'_id':i})
        if not ts:
            mdb.totalstat.save({'_id':i, 
                'maxclick':clicks, 'maxclickday':daystr, 
                'maxview':0, 'maxviewday':daystr,
                'totalclick': clicks, 'totalview':0})
        else:
            mdb.totalstat.update({'_id':i}, {'$inc':{'totalclick':clicks}})
            if 'maxclick' in ts and clicks > ts['maxclick']:
                mdb.totalstat.update({'_id':i}, {'$set':{'maxclick':clicks, 'maxclickday':daystr}})
            
        

def main(logfile):
    rdb = redis.StrictRedis(host='127.0.0.1', port=56379, db=7)
    rdb.flushdb()
    mdb = pymongo.Connection('127.0.0.1:57324').saystat
    fn = open(logfile)
    click_stat(fn, rdb, mdb)


def test_parse_line():
    a = '114.80.133.7 say.23201.com [01/Dec/2011:22:27:56 +0800] "GET /url?url=http://item.taobao.com/item.htm?id=12852441155&price=730.0&seller_nick=%E6%B7%98%E9%87%91%E7%94%B5%E5%95%86 HTTP/1.1" 301 0 "http://say.23201.com/say/v1/iframe.html?seller_nick=%E6%B7%98%E9%87%91%E7%94%B5%E5%95%86" "Mozilla/5.0 (Ubuntu; X11; Linux x86_64; rv:8.0) Gecko/20100101 Firefox/8.0" "-"'
    print parse_line(a)


if __name__ == '__main__':
    main(sys.argv[1])
    #test_parse_line()


